VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Connect"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Description = "VB TabOrder Window"
Option Explicit

Implements IDTExtensibility

Public WithEvents PrjHandler  As VBProjectsEvents          'projects event handler
Attribute PrjHandler.VB_VarHelpID = -1
Public WithEvents CmpHandler  As VBComponentsEvents        'components event handler
Attribute CmpHandler.VB_VarHelpID = -1
Public WithEvents CtlHandler  As VBControlsEvents          'controls event handler
Attribute CtlHandler.VB_VarHelpID = -1
Public WithEvents MenuHandler As CommandBarEvents          'command bar event handler
Attribute MenuHandler.VB_VarHelpID = -1
Dim mcbMenuCommandBar         As Office.CommandBarControl  'command bar object

Const guidMYTOOL$ = "{B7AFC8D0-EBE5-11cf-A497-00A0C911E8B0}"

Public NonModalApp As Boolean                              'used by AddIn Toolbar
Attribute NonModalApp.VB_VarMemberFlags = "40"

Sub Show()
Attribute Show.VB_MemberFlags = "40"
  On Error GoTo ShowErr
  
  gwinWindow.Visible = True
  gdocTabOrder.RefreshList 3
  
  Exit Sub
ShowErr:
  MsgBox Err.Description
End Sub

Private Sub Class_Initialize()
  NonModalApp = True   'used by addin toolbar
End Sub

'------------------------------------------------------
'this method adds the Add-In to the VB Tools menu
'it is called by the VB addin manager
'------------------------------------------------------
Private Sub IDTExtensibility_OnConnection(ByVal VBInst As Object, ByVal ConnectMode As vbext_ConnectMode, ByVal AddInInst As VBIDE.AddIn, custom() As Variant)
  On Error GoTo IDTExtensibility_OnConnectionErr
  
  Dim aiTmp As AddIn
  
  'save the vb instance
  Set gVBInstance = VBInst

  If Not gwinWindow Is Nothing Then
    'already running so just show it
    Show
    If ConnectMode = vbext_cm_AfterStartup Then
      'started from the addin manager
      AddToCommandBar
    End If
    Exit Sub
  End If
  
  'create the tool window
  If ConnectMode = vbext_cm_External Then
    'need to see if it is already running
    On Error Resume Next
    Set aiTmp = gVBInstance.Addins("TabOrder.Connect")
    On Error GoTo IDTExtensibility_OnConnectionErr
    If aiTmp Is Nothing Then
      'app is not in the VBADDIN.INI file so it is not in the collection
      'so lets attempt to use the 1st addin in the collection just
      'to get this app running and if there are none, an error
      'will occur and this app will not run
       Set gwinWindow = gVBInstance.Windows.CreateToolWindow(gVBInstance.Addins(1), "TabOrder.docTabOrder", LoadResString(10), guidMYTOOL$, gdocTabOrder)
    Else
      If aiTmp.Connect = False Then
        Set gwinWindow = gVBInstance.Windows.CreateToolWindow(aiTmp, "TabOrder.docTabOrder", LoadResString(10), guidMYTOOL$, gdocTabOrder)
      End If
    End If
  Else
    'must've been called from addin mgr
    Set gwinWindow = gVBInstance.Windows.CreateToolWindow(AddInInst, "TabOrder.docTabOrder", LoadResString(10), guidMYTOOL$, gdocTabOrder)
  End If

  'sink the project, components and controls event handler
  Set Me.PrjHandler = gVBInstance.Events.VBProjectsEvents
  Set Me.CmpHandler = gVBInstance.Events.VBComponentsEvents(Nothing)
  Set Me.CtlHandler = gVBInstance.Events.VBControlsEvents(Nothing, Nothing)
  
  If ConnectMode = vbext_cm_External Then
    'started from the addin toolbar
    Show
  ElseIf ConnectMode = vbext_cm_AfterStartup Then
    'started from the addin manager
    AddToCommandBar
  End If

  Exit Sub
  
IDTExtensibility_OnConnectionErr:
  MsgBox Err.Description
  
End Sub

'------------------------------------------------------
'this event removes the commandbar menu
'it is called by the VB addin manager
'------------------------------------------------------
Private Sub IDTExtensibility_OnDisconnection(ByVal RemoveMode As vbext_DisconnectMode, custom() As Variant)
  On Error GoTo IDTExtensibility_OnDisconnectionErr
  'delete the command bar entry
  mcbMenuCommandBar.Delete
  
  'save the form state for next time VB is loaded
  If gwinWindow.Visible Then
    SaveSetting APP_CATEGORY, App.Title, "DisplayOnConnect", "1"
  Else
    SaveSetting APP_CATEGORY, App.Title, "DisplayOnConnect", "0"
  End If
  
  Set gwinWindow = Nothing
  
IDTExtensibility_OnDisconnectionErr:
  
End Sub

'this event fires when the IDE is fully loaded
Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
  AddToCommandBar
End Sub

Private Sub IDTExtensibility_OnAddInsUpdate(custom() As Variant)
  'comment needed to keep this stub code from
  'being removed by VBA on compile
End Sub

'this event fires when the command bar control is clicked in the IDE
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
  Show
End Sub

'this event fires when a control is added to the current form in the IDE
Private Sub CtlHandler_ItemAdded(ByVal VBControl As VBIDE.VBControl)
  If gwinWindow.Visible Then
    gdocTabOrder.ControlAdded VBControl
  End If
End Sub

'this event fires when a control is renamed on the current form in the IDE
Private Sub CtlHandler_ItemRenamed(ByVal VBControl As VBIDE.VBControl, ByVal OldName As String, ByVal OldIndex As Long)
  If gwinWindow.Visible Then
    gdocTabOrder.ControlRenamed VBControl, OldName, OldIndex
  End If
End Sub

'this event fires when a control is removed from the current form in the IDE
Private Sub CtlHandler_ItemRemoved(ByVal VBControl As VBIDE.VBControl)
  If gwinWindow.Visible Then
    gdocTabOrder.ControlRemoved VBControl
  End If
End Sub

'this event fires when a form becomes activated in the IDE
Private Sub CmpHandler_ItemActivated(ByVal VBComponent As VBIDE.VBComponent)
  On Error GoTo CmpHandler_ItemActivatedErr
  If gwinWindow.Visible Then
    gdocTabOrder.RefreshList 0
  End If
CmpHandler_ItemActivatedErr:
End Sub

'this event fires when a form is selected in the project window
Private Sub CmpHandler_ItemSelected(ByVal VBComponent As VBIDE.VBComponent)
  CmpHandler_ItemActivated VBComponent
End Sub

Sub AddToCommandBar()
Attribute AddToCommandBar.VB_MemberFlags = "40"
  On Error GoTo AddToCommandBarErr
  
  'make sure the standard toolbar is visible
  gVBInstance.CommandBars(2).Visible = True
  
  'add it to the command bar
  'the following line will add the TabOrder manager to the
  'Standard toolbar to the right of the ToolBox button
  Set mcbMenuCommandBar = gVBInstance.CommandBars(2).Controls.Add(1, , , gVBInstance.CommandBars(2).Controls.Count)
  'set the caption
  mcbMenuCommandBar.Caption = LoadResString(200)
  'copy the icon to the clipboard
  Clipboard.SetData LoadResPicture(1000, 0)
  'set the icon for the button
  mcbMenuCommandBar.PasteFace
  
  'sink the event
  Set Me.MenuHandler = gVBInstance.Events.CommandBarEvents(mcbMenuCommandBar)
  
  'restore the last state
  If GetSetting(APP_CATEGORY, App.Title, "DisplayOnConnect", "0") = "1" Then
    'set this to display the form on connect
    Me.Show
  End If
  
  Exit Sub
    
AddToCommandBarErr:
  MsgBox Err.Description
End Sub

Private Sub PrjHandler_ItemRemoved(ByVal VBProject As VBIDE.VBProject)
  'this takes care of the user removing the only project
  If gwinWindow.Visible Then
    gdocTabOrder.RefreshList 0
  End If
End Sub
